##################################
#   __ _                         #
#  / /(_) ___ ___ _ __  ___  ___ #
# / / | |/ __/ _ \ '_ \/ __|/ _ \#
#/ /__| | (_|  __/ | | \__ \  __/#
#\____/_|\___\___|_| |_|___/\___|#
##################################

#    kazami is an othello game, written in Python, using the pygame library.
#    The word "kazami" means "weather vane" in Japanese.
#    Copyright (C) 2012  Nadia "Atarun" Cerezo
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

############################################################################################
#   _        _                        _          _                                 _       #
#  /_\   ___| | ___ __   _____      _| | ___  __| | __ _  ___ _ __ ___   ___ _ __ | |_ ___ #
# //_\\ / __| |/ / '_ \ / _ \ \ /\ / / |/ _ \/ _` |/ _` |/ _ \ '_ ` _ \ / _ \ '_ \| __/ __|#
#/  _  \ (__|   <| | | | (_) \ V  V /| |  __/ (_| | (_| |  __/ | | | | |  __/ | | | |_\__ \#
#\_/ \_/\___|_|\_\_| |_|\___/ \_/\_/ |_|\___|\__,_|\__, |\___|_| |_| |_|\___|_| |_|\__|___/#
#                                                  |___/                                   #
############################################################################################

#    This program was developed using Eclipse and PyDev.
#
#    It is hosted on Google Code.
#    http://code.google.com/p/kazami/
#
#    The picture used as texture for the interface comes from stock.xchng,
#    it is called Clean Wood and was uploaded by jetmix (who has been notified of its use).
#    http://www.sxc.hu/photo/1407481
#
#    The font used is called Shojumaru and can be used freely for any use,
#    including commercial use, courtesy of the Astigmatic One Eye Typographic Institute.
#    http://www.fontspace.com/astigmatic-one-eye-typographic-institute/shojumaru
#
#    The ASCII art found in this source code has been generated by the online app
#    TAAG (Text to ASCII Art Generator) from patorjk.com, using the Ogre font option.
#    http://patorjk.com/software/taag/

#############################################
#  _____                            _       #
#  \_   \_ __ ___  _ __   ___  _ __| |_ ___ #
#   / /\/ '_ ` _ \| '_ \ / _ \| '__| __/ __|#
#/\/ /_ | | | | | | |_) | (_) | |  | |_\__ \#
#\____/ |_| |_| |_| .__/ \___/|_|   \__|___/#
#                 |_|                       #
#############################################

import os, pygame, sys

###############################################
#   ___                _              _       #
#  / __\___  _ __  ___| |_ __ _ _ __ | |_ ___ #
# / /  / _ \| '_ \/ __| __/ _` | '_ \| __/ __|#
#/ /__| (_) | | | \__ \ || (_| | | | | |_\__ \#
#\____/\___/|_| |_|___/\__\__,_|_| |_|\__|___/#
###############################################

# Filenames
ASSET_FOLDER = "resources"+os.sep
BACKGROUND_FILENAME = "background.png"
BOARD_FILENAME = "board.png"
FONT_FILENAME = "Shojumaru.ttf"
SCORES_FILENAME = "scores.png"

# Dimensions
BOARD_SIZE = 556
MENU_BUTTON_HEIGHT = 50
MENU_WIDTH = 200
SCORES_FONT_SIZE = 24
SCORES_HEIGHT = 114
SCORES_WIDTH = 200
WINDOW_HEIGHT = 768
WINDOW_WIDTH = 1024

# Coordinates
BLACK_SCORE_LEFT = 189
BLACK_SCORE_TOP = 441
BOARD_LEFT = 344
BOARD_TOP = 153
MENU_LEFT = 40
MENU_TOP = 200
SCORES_LEFT = 40
SCORES_TOP = 400
WHITE_SCORE_LEFT = 190
WHITE_SCORE_TOP = 475

# Miscellaneous
SCORES_COLOR = (0,0,0)

#######################################################
#   ___                     _     ___ _               #
#  / __\ ___   __ _ _ __ __| |   / __\ | __ _ ___ ___ #
# /__\/// _ \ / _` | '__/ _` |  / /  | |/ _` / __/ __|#
#/ \/  \ (_) | (_| | | | (_| | / /___| | (_| \__ \__ \#
#\_____/\___/ \__,_|_|  \__,_| \____/|_|\__,_|___/___/#
#######################################################

class Board(object):
    def __init__(self):
        Board.BLACK = "B"
        Board.EMPTY = "E"
        Board.WHITE = "W"
        self.scores = {Board.BLACK : 2, Board.WHITE : 2}
    
    def get_score(self, player):
        if player in self.scores.keys():
            return self.scores[player]
        else:
            return None

###################################################
#                             ___ _               #
#  /\/\   ___ _ __  _   _    / __\ | __ _ ___ ___ #
# /    \ / _ \ '_ \| | | |  / /  | |/ _` / __/ __|#
#/ /\/\ \  __/ | | | |_| | / /___| | (_| \__ \__ \#
#\/    \/\___|_| |_|\__,_| \____/|_|\__,_|___/___/#
###################################################

class Menu(object):
    def __init__(self):
        Menu.NEW = "new"
        Menu.PASS = "pass"
        Menu.QUIT = "quit"
        Menu.LIST = ["new", "pass", "quit"]
        
        Menu.BUTTON_SUFFIX = "_button.png"
        Menu.HOVER_SUFFIX = "_hover.png"
        Menu.ACTIVE_SUFFIX = "_active.png"
        Menu.LOCKED_SUFFIX = "_locked.png"
        
        Menu.CURRENT = None
        
        Menu.IMG_BUTTON = {button : pygame.image.load(ASSET_FOLDER+button+Menu.BUTTON_SUFFIX) for button in Menu.LIST}
        Menu.IMG_HOVER = {button : pygame.image.load(ASSET_FOLDER+button+Menu.HOVER_SUFFIX) for button in Menu.LIST}
        Menu.IMG_ACTIVE = {button : pygame.image.load(ASSET_FOLDER+button+Menu.ACTIVE_SUFFIX) for button in Menu.LIST}
        Menu.IMG_LOCKED = {button : pygame.image.load(ASSET_FOLDER+button+Menu.LOCKED_SUFFIX) for button in Menu.LIST}
        self.img = {Menu.NEW : Menu.IMG_BUTTON[Menu.NEW], Menu.PASS : Menu.IMG_LOCKED[Menu.PASS], Menu.QUIT : Menu.IMG_BUTTON[Menu.QUIT]}
        self.states = {Menu.NEW : True, Menu.PASS : False, Menu.QUIT : True}
    
    def hover(self, button):
        if button in Menu.LIST and button != Menu.CURRENT and menu.states[button]:
            if Menu.CURRENT != None:
                menu.img[Menu.CURRENT] = Menu.IMG_BUTTON[Menu.CURRENT]
            menu.img[button] = Menu.IMG_HOVER[button]
            Menu.CURRENT = button

#####################
#   _     _____     #
#  /_\    \_   \___ #
# //_\\    / /\/ __|#
#/  _  \/\/ /_ \__ \#
#\_/ \_/\____/ |___/#
#####################

###########################################################
#              _            ___ _           _             #
#  /\/\   __ _(_)_ __      /   (_)___ _ __ | | __ _ _   _ #
# /    \ / _` | | '_ \    / /\ / / __| '_ \| |/ _` | | | |#
#/ /\/\ \ (_| | | | | |  / /_//| \__ \ |_) | | (_| | |_| |#
#\/    \/\__,_|_|_| |_| /___,' |_|___/ .__/|_|\__,_|\__, |#
#                                    |_|            |___/ #
###########################################################

def display():
    # Background
    surf_window.blit(img_background, (0,0))
    
    # Menu
    for button in Menu.LIST:
        surf_window.blit(menu.img[button], rect_buttons[button])
    
    # Scores
    surf_window.blit(img_scores, (SCORES_LEFT, SCORES_TOP))
    surf_black_score = scores_font.render(str(board.get_score(Board.BLACK)), True, SCORES_COLOR)
    surf_window.blit(surf_black_score, (BLACK_SCORE_LEFT, BLACK_SCORE_TOP))
    surf_white_score = scores_font.render(str(board.get_score(Board.WHITE)), True, SCORES_COLOR)
    surf_window.blit(surf_white_score, (WHITE_SCORE_LEFT, WHITE_SCORE_TOP))
    
    # Board
    surf_window.blit(img_board, (BOARD_LEFT, BOARD_TOP))
    

#################################################################################
#                                 ___                             _             #
#  /\/\   ___  _   _ ___  ___    / _ \_ __ ___   ___ ___  ___ ___(_)_ __   __ _ #
# /    \ / _ \| | | / __|/ _ \  / /_)/ '__/ _ \ / __/ _ \/ __/ __| | '_ \ / _` |#
#/ /\/\ \ (_) | |_| \__ \  __/ / ___/| | | (_) | (_|  __/\__ \__ \ | | | | (_| |#
#\/    \/\___/ \__,_|___/\___| \/    |_|  \___/ \___\___||___/___/_|_| |_|\__, |#
#                                                                         |___/ #
#################################################################################

def hover():
    # Menu
    if rect_menu.collidepoint(mouse_x, mouse_y):
        index = int((mouse_y - MENU_TOP)/MENU_BUTTON_HEIGHT)
        menu.hover(Menu.LIST[index])

#############################################################
#  _____       _ _   _       _ _          _   _             #
#  \_   \_ __ (_) |_(_) __ _| (_)______ _| |_(_) ___  _ __  #
#   / /\/ '_ \| | __| |/ _` | | |_  / _` | __| |/ _ \| '_ \ #
#/\/ /_ | | | | | |_| | (_| | | |/ / (_| | |_| | (_) | | | |#
#\____/ |_| |_|_|\__|_|\__,_|_|_/___\__,_|\__|_|\___/|_| |_|#
#############################################################

# pygame library
pygame.init()

# Mouse
mouse_x, mouse_y = 0, 0
mouse_pressed = False

# Board
board = Board()

# Menu
menu = Menu()

# Window
surf_window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))

# Images
img_background = pygame.image.load(ASSET_FOLDER+BACKGROUND_FILENAME)
img_board = pygame.image.load(ASSET_FOLDER+BOARD_FILENAME)
img_scores = pygame.image.load(ASSET_FOLDER+SCORES_FILENAME)

# Rectangles (for collision detection)
rect_board = pygame.Rect(BOARD_LEFT, BOARD_TOP, BOARD_SIZE, BOARD_SIZE)
rect_menu = pygame.Rect(MENU_LEFT, MENU_TOP, MENU_WIDTH, MENU_BUTTON_HEIGHT * len(Menu.LIST))
rect_buttons = {Menu.LIST[i] : pygame.Rect(MENU_LEFT, MENU_TOP + MENU_BUTTON_HEIGHT * i, MENU_WIDTH, MENU_BUTTON_HEIGHT) for i in range(0,len(Menu.LIST))}

# Font
scores_font = pygame.font.Font(ASSET_FOLDER+FONT_FILENAME, SCORES_FONT_SIZE)

#################################################
#              _           __                   #
#  /\/\   __ _(_)_ __     / /  ___   ___  _ __  #
# /    \ / _` | | '_ \   / /  / _ \ / _ \| '_ \ #
#/ /\/\ \ (_| | | | | | / /__| (_) | (_) | |_) |#
#\/    \/\__,_|_|_| |_| \____/\___/ \___/| .__/ #
#                                        |_|    #
#################################################

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.MOUSEMOTION:
            mouse_x, mouse_y = event.pos
            hover()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            mouse_pressed = True
        elif event.type == pygame.MOUSEBUTTONUP:
            mouse_pressed = False
    display()
    pygame.display.update()
